//
//  ConverPointVC.m
//  DYContact
//
//  Created by ghwang on 2018/9/6.
//  Copyright © 2018年 ghwang. All rights reserved.
//

#import "ConverPointVC.h"
#import <MapKit/MapKit.h>
#import "LSListAnnotation.h"
#import "XMLocationManager.h"
#import <EasyNavigation/EasyNavigation.h>
#import "SDAutoLayout.h"
#import "BaseSwift-Swift.h"
#import "MacosHeader.h"



#define kUserSpan MKCoordinateSpanMake(0.009, 0.009)
#define kUserSpanAround MKCoordinateSpanMake(0.02*0.03, 0.02*0.03)

@interface ConverPointVC ()<MKMapViewDelegate>{
    
    BOOL _isAgain;
    LSListAnnotation *ani ;
}
@property (nonatomic,assign) CLLocationCoordinate2D  coord;
@property (strong,nonatomic) MKMapView *mapview;
@end

@implementation ConverPointVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor backgroundColor];

    [self.navigationView setTitle:@"选择经纬度"];


    // 1.添加地图
    MKMapView *mapView = [[MKMapView alloc] init];
    mapView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
    // 显示用户的位置点
    mapView.showsUserLocation = NO;
    // 设置代理
    mapView.delegate = self;
    self.mapview = mapView;
    [self.view addSubview:mapView];
    
    mapView.sd_layout.spaceToSuperView(UIEdgeInsetsMake(HT_StatusBarAndNavigationBarHeight, 0, 0, 0));
    
    
    if ([self.type isEqualToString:@"show"]) {
        
        [self.navigationView setTitle:@"地图"];

        CLLocationCoordinate2D cor = CLLocationCoordinate2DMake([self.lat floatValue],[self.lnt floatValue]);
        LSListAnnotation *ani = [[LSListAnnotation alloc] initWithCoordinates:cor title:self.addr subTitle:@""];
        [self.mapview addAnnotation:ani];
        
        [self.mapview setCenterCoordinate:cor animated:YES];
        
        MKCoordinateRegion region = MKCoordinateRegionMake(cor, kUserSpan);
        [self.mapview setRegion:region animated:YES];
        
        return;
    }
    
    UITapGestureRecognizer *mTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapPress:)];
    mTap.numberOfTapsRequired = 1;
    mTap.numberOfTouchesRequired = 1;
    [self.mapview addGestureRecognizer:mTap];
    
    [self.navigationView addRightButtonWithTitle:@"确定" clickCallBack:^(UIView *view) {
        
        self -> _coord = [[XMLocationManager shareManager] getBaiDuCoordinateByGaoDeCoordinate:self -> _coord];
        NSString *lat = [NSString stringWithFormat:@"%f",self->_coord.latitude];
        NSString *longt = [NSString stringWithFormat:@"%f",self->_coord.longitude];
        
        self.CallBack([NSString stringWithFormat:@"%@,%@",lat,longt]);
        
        [self.navigationController popViewControllerAnimated:YES];
        
    }];
    
    [[XMLocationManager shareManager] requestAuthorization];
    
    if ([XMLocationManager shareManager].latitude==0 || [XMLocationManager shareManager].longitude==0) {
        
        [[XMLocationManager shareManager] startLocation];
        [XMLocationManager shareManager].locationCompleteBlock = ^(CLLocationCoordinate2D coordinate2D) {
            
            MKCoordinateRegion region = MKCoordinateRegionMake(coordinate2D, kUserSpan);
            [self.mapview setRegion:region animated:YES];
            //[self.mapview setCenterCoordinate:coordinate2D animated:YES];
            
        };
    }else{
        
        CLLocationCoordinate2D cor = CLLocationCoordinate2DMake([XMLocationManager shareManager].latitude, [XMLocationManager shareManager].longitude);
        MKCoordinateRegion region = MKCoordinateRegionMake(cor, kUserSpan);
        [self.mapview setRegion:region animated:YES];

        
        
    }
}
- (void)tapPress:(UIGestureRecognizer*)gestureRecognizer {
    
    if (ani) {
     
        [self.mapview removeAnnotation:ani];
    }
    
    CGPoint touchPoint = [gestureRecognizer locationInView:self.mapview];
    CLLocationCoordinate2D touchMapCoordinate =
    [self.mapview convertPoint:touchPoint toCoordinateFromView:self.mapview];
    self.coord = touchMapCoordinate;
    ani = [[LSListAnnotation alloc] initWithCoordinates:touchMapCoordinate title:@"" subTitle:@""];
    [self.mapview addAnnotation:ani];
    

    [self.mapview setCenterCoordinate:touchMapCoordinate animated:YES];
    

    
}

/*-(MKAnnotationView*)mapView:(MKMapView *)mapView viewForAnnotation:(LSListAnnotation *)annotation
{
    if ([annotation isKindOfClass:[LSListAnnotation class]])
    {
        static NSString *pointReuseIndetifier = @"pointReuseIndetifier";
        MKAnnotationView *annotationView = (MKAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:pointReuseIndetifier];
        
        if (annotationView == nil)
        {
            annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:pointReuseIndetifier];
            annotationView.canShowCallout= NO;      //设置气泡可以弹出，默认为NO
        }
        annotationView.image = [UIImage imageNamed:@"position_icon"];
        return annotationView;
    }
    return nil;
}*/
#pragma mark 大头针样式
-(MKAnnotationView*)mapView:(MKMapView *)mapView viewForAnnotation:(LSListAnnotation *)annotation
{
    if ([annotation isKindOfClass:[LSListAnnotation class]])
    {
        static NSString *pointReuseIndetifier = @"pointReuseIndetifier";
        MKAnnotationView *annotationView = (MKAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:pointReuseIndetifier];
        
        if (annotationView == nil)
        {
            annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:pointReuseIndetifier];
            annotationView.canShowCallout= YES;      //设置气泡可以弹出，默认为NO
            //            annotationView.animatesDrop = YES;       //设置标注动画显示，默认为NO
            //            annotationView.draggable = YES;           //设置标注可以拖动，默认为NO
            UIButton *but = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
            [but addTarget:self action:@selector(drawLine) forControlEvents:    UIControlEventTouchUpInside];
            annotationView.rightCalloutAccessoryView= but;  //设置气泡右侧按钮
        }
        annotationView.image = [UIImage imageNamed:@"addr"];
        return annotationView;
    }
    return nil;
}
- (void)drawLine
{
    NSDictionary *options = @{
                              MKLaunchOptionsDirectionsModeKey :
                                  MKLaunchOptionsDirectionsModeDriving
                              };
    
    MKMapItem *startItem = [MKMapItem mapItemForCurrentLocation];
    
    MKPlacemark *endPlacemark = [[MKPlacemark alloc] initWithCoordinate:self.coord addressDictionary:nil];
    
    MKMapItem *endItem = [[MKMapItem alloc]
                          initWithPlacemark:endPlacemark];
    
    NSArray *directionsItems = @[startItem, endItem];
    [MKMapItem openMapsWithItems:directionsItems launchOptions:options];
}


#pragma mark 点击大头针
- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view
{
    LSListAnnotation *ann = view.annotation;
    self.coord = CLLocationCoordinate2DMake(ann.coordinate.latitude, ann.coordinate.longitude);
    [mapView setCenterCoordinate:ann.coordinate animated:YES];
}

@end
